package com.example.javadynamiccompile;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author 陳樂
 * @version 1.0.0
 * @ClassName Test.java
 * @Description TODO
 * @createTime 2023年03月08日 22:27:00
 */
public class Test {


    public static void main(String[] args) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null, null, null, "F:\\code\\java-coder\\java-dynamic-compile\\src\\main\\java\\com\\example\\javadynamiccompile\\A.java");
        System.out.println("result=" + result);

        try{
            if (result == 0) {
                Runtime run = Runtime.getRuntime();// 运行
                Process process = run.exec("java -cp F:\\code\\java-coder\\java-dynamic-compile\\src\\main\\java\\com\\example\\javadynamiccompile\\ A");

                InputStream is = process.getInputStream();// 获取运行后输出结果
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String info = "";
                while ((info = br.readLine()) != null) {
                    System.out.println(info);
                }
            }
        } catch (Exception e) {

        }
    }

}
